<?php
namespace Common\Org\Sms;
class Sms
{
    /*******创蓝VIP对接****/
    protected $account;
    protected $password;

    public function __construct($driver, $config = array())
    {
        $config = D('SystemStore')->load(1);
        $setting = json_decode($config['sms_setting'], true);
        $class = strpos($driver, '\\') ? $driver : 'Common\Org\\Sms\\Driver\\' . ucfirst(($driver));
        $this->setDriver($class, $setting);
    }

    /**
     * 短信发送接口
     * @return array 结果数组
     */
    public function send($mobile, $msg, $limit = 0)
    {
        if ($this->limit(10, 180) || $limit) { //一个ip ,在180秒内只能调用10次
            $r = $this->payer->send($mobile, $msg);
            return $r;
        } else {
            return array('error' => 4, 'msg' => '调用超限');
        }
    }


    /********ip调用限制****/
    public function limit($max = 10, $exp = 100)
    {
        $ip = ip2long(getIp());
        $key = "sms_" . $ip;
        $v = S($key);
        $v = $v ? $v : 0;
        if ($v > $max) {
            return false;
        } else {
            $v++;
            S($key, $v, $exp);
            return true;
        }
    }

    /**
     * 设置支付驱动
     * @param string $class 驱动类名称
     */
    private function setDriver($class, $config)
    {
        $this->payer = new $class($config);
        if (!$this->payer) {
            E("短信驱动不正确：{$class}");
        }
    }


}